#ifndef GRIDTABLEWIDGET_H
#define GRIDTABLEWIDGET_H

#include <QWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>
#include <QLineEdit>
#include <QPushButton>
#include <QPainter>
#include "CommonContainer.h"

class GridTableWidget : public CommonContainer
{
    Q_OBJECT

public:
    struct GridTableItem {
        QString name;          // 名称
        QString value;         // 输入值
    };

    explicit GridTableWidget(const QString &title, QWidget *parent = nullptr);
    ~GridTableWidget();
    
    // 添加表格项
    void addGridTableItem(const GridTableItem &item);
    void addGridTableItem(const QString &name, const QString &value = QString());
    
    // 移除表格项
    void removeGridTableItem(int index);
    void clearGridTableItems();
    
    // 获取表格项
    GridTableItem getGridTableItem(int index) const;
    QVector<GridTableItem> getAllGridTableItems() const;
    
    // 设置表格项值
    void setGridTableName(int index, const QString &name);
    void setGridTableValue(int index, const QString &value);
    QString getGridTableName(int index) const;
    QString getGridTableValue(int index) const;
    
    // 编辑模式
    void setEditMode(bool enabled);
    bool isEditMode() const;

protected:
    void paintEvent(QPaintEvent *event) override;

signals:
    void gridTableNameChanged(int index, const QString &name);
    void gridTableValueChanged(int index, const QString &value);
    void editModeChanged(bool enabled);

private slots:
    void onValueChanged();
    void onEditButtonClicked();
    void onEditModeChanged(bool enabled);

private:
    void setupUI();
    void setupConnections();
    void updateLayout();
    void drawGridBorders(QPainter &painter);
    
    // 布局组件
    QGridLayout *m_gridLayout;
    QWidget *m_gridWidget;
    
    // 表格项
    QVector<GridTableItem> m_gridTableItems;
    QVector<QLineEdit*> m_nameEdits;
    QVector<QLineEdit*> m_valueEdits;
    
    // 状态
    bool m_isEditMode;
    int m_gridColumns;
    int m_gridRows;
};

#endif // GRIDTABLEWIDGET_H
